/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Visit.java
 *
 * Created on Dec 27, 2008, 1:46:47 AM
 */

package GUI;

import Dictionary.Dictionary;
import stuff.ItemComparator;
import functions.ItemList;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.SpringLayout;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.text.StyledDocument;
import model.Item;
import model.Diagnose;
import model.Drug;
import model.Operation;
import model.Visit;

/**
 *
 * @author JArik
 */
public class VisitInfo extends javax.swing.JFrame {

    private MainFrame frame;
    private ArrayList<Object> visits;
    private ArrayList<ItemList> pureVisits;
    private UserCard userCard;
    private ItemList<Item> visit;
    private JPanel diagPanel, drugPanel, operPanel;
    private JTable diagTable, drugTable, operTable;
    /** Creates new form Visit */
    public VisitInfo(MainFrame nFrame, ArrayList<ItemList<Item>> nVisits, UserCard selV ) {
        initComponents();
        frame = nFrame;
        visits = (ArrayList)nVisits;
        userCard = selV;
        pureVisits = new ArrayList<ItemList>();
        if( visits != null && visits.size() > 0)
          fillPriem();
        else
        {
            this.removeAll();
            this.remove(priem);
            GridLayout grid = new GridLayout(0,1);
            
            this.getContentPane().setLayout(grid);
            //this.setLayout(grid);
            JLabel label = new JLabel("<html><center><font color=grey>Выделите визиты, которые хотите посмотреть более детально.</font></center>");
            this.add(label);
            label.setSize(this.getWidth(), this.getHeight());
            
        }
        pack();
        
        
    }

    private void fillPriem() {
        JPanel visitPanel;
        JScrollPane scroll;
        BoxLayout layout;
        for(int i=0; i< visits.size(); i++)
        {
           //drugPanel.add(new JLabel("Выданные лекарства:"));
           visitPanel = new JPanel();
           scroll = new JScrollPane();
           scroll.getVerticalScrollBar().setUnitIncrement(20);
           
           diagPanel = new JPanel();
           operPanel = new JPanel();
           drugPanel = new JPanel();
           drugPanel.setLayout(new BoxLayout(drugPanel, BoxLayout.PAGE_AXIS));
           diagPanel.setLayout(new BoxLayout(diagPanel, BoxLayout.PAGE_AXIS));
           operPanel.setLayout(new BoxLayout(operPanel, BoxLayout.PAGE_AXIS));

           drugPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, Dictionary.DRUGS, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 1, 15), new java.awt.Color(0, 0, 0))); // NOI18N
           diagPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, Dictionary.DIAGNOSES, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 1, 15), new java.awt.Color(0, 0, 0))); // NOI18N
           operPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, Dictionary.OPERATIONS, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 1, 15), new java.awt.Color(0, 0, 0))); // NOI18N

           DefaultTableModel model;
           diagTable = new JTable(getNewDiagModel());
           diagTable.setAutoCreateRowSorter(true);
           diagTable.setBackground(Color.getHSBColor(50, 0, 90));
           diagTable.getColumnModel().getColumn(0).setMaxWidth(70);
           diagTable.getColumnModel().getColumn(2).setMaxWidth(60);
           diagTable.getColumnModel().getColumn(3).setMaxWidth(200);
           diagTable.getColumnModel().getColumn(3).setPreferredWidth(200);
           diagTable.setFont(new java.awt.Font("Tahoma", 1, 14));
           diagTable.setRowHeight(20);
           model = (DefaultTableModel)diagTable.getModel();
           model.setRowCount(0);
           diagPanel.add(diagTable.getTableHeader());
           diagPanel.add(diagTable);

           drugTable = new JTable(getNewDrugModel());
           drugTable.setAutoCreateRowSorter(true);
           drugTable.setBackground(Color.getHSBColor(50, 0, 90));
           drugTable.getColumnModel().getColumn(0).setMaxWidth(70);
           drugTable.getColumnModel().getColumn(2).setMaxWidth(60);
           drugTable.getColumnModel().getColumn(3).setMaxWidth(70);
           drugTable.getColumnModel().getColumn(4).setMaxWidth(200);
           drugTable.getColumnModel().getColumn(4).setPreferredWidth(200);
           drugTable.setFont(new java.awt.Font("Tahoma", 1, 14));
           drugTable.setRowHeight(20);
           model = (DefaultTableModel)drugTable.getModel();
           model.setRowCount(0);
           drugPanel.add(drugTable.getTableHeader());
           drugPanel.add(drugTable);

           operTable = new JTable(getNewOperModel());
           operTable.setAutoCreateRowSorter(true);
           operTable.setBackground(Color.getHSBColor(50, 0, 90));
           operTable.setFont(new java.awt.Font("Tahoma", 1, 14));
           operTable.setRowHeight(20);
           model = (DefaultTableModel)operTable.getModel();
           model.setRowCount(0);
           operPanel.add(operTable.getTableHeader());
           operPanel.add(operTable);

           layout = new BoxLayout(visitPanel, BoxLayout.PAGE_AXIS);
           visitPanel.setLayout(layout);
           visitPanel.setSize(templatePanel.getSize());
           
           if( visits.get(i) instanceof ItemList)
           {
               visit = (ItemList<Item>)visits.get(i);
               for (int y=0; y < visit.size(); y++)
               {
                 //System.out.println(y);
                 createVisitPanel(visit.get(y),visitPanel);
               }
               scroll.setViewportView(visitPanel);

               pureVisits.add(visit);
               priem.add(visits.get(i).toString(),scroll);
               
           }
           layout.invalidateLayout(visitPanel);
        }
        
    }

    private void createVisitPanel(Item vis,JPanel panel)
    {
           if (vis instanceof Visit )// Visit
           {
               Visit theVis = (Visit)vis;
               JTextArea descr = new JTextArea();

               //JScrollPane scroll = new JScrollPane();
               descr.setEditable(false);
               
               //descr
               descr.setFont(new Font("Tahoma", Font.BOLD, 14));

               descr.setBackground(panel.getBackground());

               //descr.setText(theVis.getDescr()+
               //             "\n---------------------------");
               descr.setText(theVis.getDescr() + 
                           "\n---------------------------" +
                           "\n"+Dictionary.STATUS+": " + theVis.getStatus() );
               
                if( theVis.getRecom() != null && !theVis.getRecom().isEmpty() )
                   descr.setText(descr.getText() +
                           "\n"+Dictionary.RECOMMENDATIONS+": " + theVis.getRecom() );
               if( theVis.getHealing().getResult() != null && !theVis.getHealing().getResult().isEmpty() )
                   descr.setText(descr.getText() +
                           "\n"+Dictionary.COURSE_RESULT+": " + theVis.getHealing().getResult() );
               
               descr.setBorder(javax.swing.BorderFactory.createTitledBorder(null, Dictionary.VISIT_DESCR, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 0, 14), new java.awt.Color(0, 0, 0))); // NOI18N
               //descr.setMinimumSize(new Dimension(0,50));

               descr.setMaximumSize(new Dimension(65553,65553));
               //descr.setPreferredSize(new Dimension(500,70));
               descr.setLineWrap(true);
               descr.setWrapStyleWord(true);
               descr.setDisabledTextColor(Color.black);
               descr.setForeground(Color.black);
               //scroll.setViewportView(descr);
               panel.add(descr);
           } else
           if (vis instanceof Drug)
           {
               Drug drug = (Drug)vis;
               DefaultTableModel model = (DefaultTableModel)drugTable.getModel();

               model.addRow(new Object[]{drug.getType(),drug,drug.getAmount(),
                                            drug.getExec().getSurname(),
                                            drug.getDescr()});
               drugTable.setModel(model);

               panel.add(drugPanel);
               //panel.add(new JSeparator());
           } 
           else if (vis instanceof Diagnose)
           {
               Diagnose diag = (Diagnose)vis;
               DefaultTableModel model = (DefaultTableModel)diagTable.getModel();
               
               model.addRow(new Object[]{diag.getType(),diag,
                                            diag.getExec().getSurname(),
                                            diag.getDescr()});
               diagTable.setModel(model);
               

               panel.add(diagPanel);
               panel.add(new JSeparator());
           } else if (vis instanceof Operation )// Operation
           {
               Operation oper = (Operation)vis;
               DefaultTableModel model = (DefaultTableModel)operTable.getModel();

               model.addRow(new Object[]{oper,
                                            oper.getExec().getSurname(),
                                            oper.getDescr()});
               operTable.setModel(model);


               panel.add(operPanel);
               panel.add(new JSeparator());
           } 
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        templatePanel = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        templateScroll = new javax.swing.JScrollPane();
        diagTablePanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        diagTemplateTable = new javax.swing.JTable();
        priem = new javax.swing.JTabbedPane();

        templatePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true), "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
        templatePanel.setLayout(new javax.swing.BoxLayout(templatePanel, javax.swing.BoxLayout.PAGE_AXIS));

        jLabel4.setText("jLabel4");

        jScrollPane1.setBorder(null);
        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        diagTemplateTable.setAutoCreateRowSorter(true);
        diagTemplateTable.setBackground(new java.awt.Color(236, 233, 216));
        diagTemplateTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Диагноз", "Установил", "Описание"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(diagTemplateTable);

        javax.swing.GroupLayout diagTablePanelLayout = new javax.swing.GroupLayout(diagTablePanel);
        diagTablePanel.setLayout(diagTablePanelLayout);
        diagTablePanelLayout.setHorizontalGroup(
            diagTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(diagTablePanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 482, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        diagTablePanelLayout.setVerticalGroup(
            diagTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addHierarchyBoundsListener(new java.awt.event.HierarchyBoundsListener() {
            public void ancestorMoved(java.awt.event.HierarchyEvent evt) {
            }
            public void ancestorResized(java.awt.event.HierarchyEvent evt) {
                formAncestorResized(evt);
            }
        });

        priem.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
        priem.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);
        priem.setAutoscrolls(true);
        priem.setFont(new java.awt.Font("Tahoma", 0, 14));
        priem.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                priemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(priem, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(priem, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
        );

        priem.getAccessibleContext().setAccessibleName("Диагнозы");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void priemStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_priemStateChanged
        userCard.setActiveVisit(pureVisits.get(priem.getSelectedIndex()) );
        
    }//GEN-LAST:event_priemStateChanged

    private void formAncestorResized(java.awt.event.HierarchyEvent evt) {//GEN-FIRST:event_formAncestorResized
        
    }//GEN-LAST:event_formAncestorResized

    /**
    * @param args the command line arguments
    */

    private DefaultTableModel getNewDiagModel()
    {

       return  new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null,null},
                {null, null, null,null},
                {null, null, null,null},
                {null, null, null,null}
            },
            new String [] {
                Dictionary.TYPE,
                Dictionary.DIAGNOSE,
                Dictionary.DOCTOR,
                Dictionary.DESCR
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false,false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        };

    }
    private DefaultTableModel getNewDrugModel()
    {
       return  new javax.swing.table.DefaultTableModel(
            new Object [][] {},
            new String [] {
                Dictionary.TYPE,
                Dictionary.DRUGS,
                Dictionary.AMOUNT_SHORT,
                Dictionary.DOCTOR,
                Dictionary.DESCR
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class, java.lang.Double.class,
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false,false,false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        };

    }
    private DefaultTableModel getNewOperModel()
    {
       return  new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                Dictionary.OPERATIONS,
                Dictionary.DOCTOR,
                Dictionary.DESCR
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        };
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel diagTablePanel;
    private javax.swing.JTable diagTemplateTable;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane priem;
    private javax.swing.JPanel templatePanel;
    private javax.swing.JScrollPane templateScroll;
    // End of variables declaration//GEN-END:variables

}
